package com.example.tuter.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.tuter.R;
import com.example.tuter.entity.Message;
import com.example.tuter.entity.Post;
import com.example.tuter.util.Constant;
import com.example.tuter.util.TimeUnitUtil;
import com.example.tuter.util.httpUtil.HttpFunction.CommunityAPI;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.HomePageViewHolder> {
    String TAG = PostAdapter.class.getSimpleName();
    public List<Post> posts;
    private Activity context;
    private final int TYPE_ITEM = 1;
    private final int TYPE_FOOTER = 2;

    public PostAdapter(List<Post> posts, Activity context) {
        this.posts = posts;
        this.context = context;
    }

    @NonNull
    @Override
    public PostAdapter.HomePageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_post, parent, false);
            return new HomePageViewHolder(view, viewType);

        } else if (viewType == TYPE_FOOTER) {
            View footview = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.no_content_more, parent, false);
            return new HomePageViewHolder(footview, viewType);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(@NonNull HomePageViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if (getItemViewType(position) == TYPE_ITEM) {
            Post post = posts.get(position);
            holder.post_username.setText(post.getUsername());
            holder.post_title.setText(post.getTitle());
            holder.post_description.setText(post.getDescription());
            String time = "丢了";
            try {
                time = TimeUnitUtil.getTimeByTimestampString(Long.valueOf(post.getCreateTime()));
            } catch (Exception ignored) {

            }

            holder.post_convert_time.setText(time);//时间转换
            holder.post_floors.setText(post.getFloors());
            holder.post_likes.setText(post.getLikes() + "");
            holder.drive.setText(getDriveName(post.getRecommendType()));

            Glide.with(context).load(post.getPic1()).override(500, 500).crossFade().diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.post_imageView);
            //添加点赞
            holder.addLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //
                    addLike(posts.get(position).getPid());
                    Post p = posts.get(position);
                    p.setLikes(p.getLikes() + 1);
                    notifyDataSetChanged();
                }
            });
        }
    }
    //获取推荐类型，如果没有，返回none
    private String getDriveName(Integer recommendType) {
        if (recommendType == null) return "";
        switch (recommendType) {
            case Constant.CF:
                return "用户推荐";
            case Constant.CB:
                return "兴趣推荐";
            case Constant.HR:
                return "热点推荐";
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return posts == null ? 0 : posts.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为FooterView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    public class HomePageViewHolder extends RecyclerView.ViewHolder {
        private TextView drive;
        private TextView post_username;
        private TextView post_floors;
        private TextView post_title;
        private TextView post_description;
        private TextView post_convert_time;
        private TextView post_likes;
        private ImageView addLike;
        private ImageView post_imageView;
        LinearLayout footView;

        public HomePageViewHolder(@NonNull View itemView, int viewType) {
            super(itemView);
            if (viewType == TYPE_ITEM) {
                post_imageView = itemView.findViewById(R.id.tie_imageView);
                post_username = itemView.findViewById(R.id.tie_username);
                post_title = itemView.findViewById(R.id.tie_title);
                post_description = itemView.findViewById(R.id.tie_description);
                post_convert_time = itemView.findViewById(R.id.tie_convert_time);
                post_floors = itemView.findViewById(R.id.ord_item_comment_number_num);
                post_likes = itemView.findViewById(R.id.ord_item_comment_likes_num);
                drive = itemView.findViewById(R.id.drive);
                addLike = itemView.findViewById(R.id.ord_item_comment_likes_text);
                itemView.setOnClickListener(view -> {
                    if (monItemClickListener != null) {
                        monItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                });
            } else if (viewType == TYPE_FOOTER) {
                footView = (LinearLayout) itemView;
            }

        }

    }

    private OnRecyclerItemClickListener monItemClickListener;

    public void setRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        monItemClickListener = listener;
    }

    public interface OnRecyclerItemClickListener {
        void onRecyclerItemClick(int position);
    }

    public void addLike(final Integer pid) {
        CommunityAPI communityAPI = Constant.r.create(CommunityAPI.class);
        Call<Message<String>> call = communityAPI.like(pid);
        call.enqueue(new Callback<Message<String>>() {
            @Override
            public void onResponse(Call<Message<String>> call, Response<Message<String>> response) {
                //点赞成功,其实应该在这里更新ui
            }

            @Override
            public void onFailure(Call<Message<String>> call, Throwable t) {

            }
        });
    }

}
